<template>
    <div>
        <allHead>
            <template v-slot:mid>教学视频生成助手</template>
        </allHead>

        <div class="bd">
            <div class="chat" v-for="item in conversation">

                <div class="user question" v-if="item.role === 'user'">
                    <span>{{ item.content }}</span>
                </div>
                <div class="bot answer" v-if="item.role === 'assistant'">
                    AI:
                    <div class="bot answer" v-if="item.content.task_status === 'SUCCEEDED'">
                        <video controls>
                            <source :src="item.content.video_url" type="video/mp4" />
                        </video>
                    </div>
                    <div class="bot answer" v-else>
                        <span v-html="item.content"></span>
                    </div>
                </div>
            </div>
            <div v-if="!finish">
                <van-loading vertical>
                    <template #icon>
                    <van-icon name="star-o" size="30" />
                    </template>
                    生成中，请稍等...
                </van-loading>
            </div>
        </div>

        <div class="ft">
            <div class="input">
                <input type="text" placeholder="请输入您想要生成的视频" v-model="question">
                <button @click="send">发送</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import allHead from "@/components/allHead.vue";
import { onMounted, ref } from 'vue'
import axios from 'axios'
import { useBookStore } from '@/stores/bookStore.js';

const bookStore = useBookStore();

const book = ref(bookStore.book.book || {})

const finish = ref(true)

const conversation = ref([
    {
        role: "assistant",
        content: `您好！我可以为您生成${book.value.editions}-${book.value.textClass}${book.value.term}-${book.value.subject}-${book.value.unit}-${book.value.content}的相关视频，您可以告诉我您需要什么样的视频，越详细越好。`
    },
])

const question = ref('')

// const isImageUrl = (url) => {
//     // 正则表达式检查 URL 是否以 http:// 或 https:// 开头
//     const httpRegex = /^https?:\/\//i;
//     return httpRegex.test(url);
// }


// 发送请求
const send = async () => {
    finish.value = false
    if (question.value.trim() === "") {
        return
    }
    conversation.value.push({
        role: "user",
        content: question.value
    })

    // 发送请求
    const msg = {
        role: "user",
        content: question.value
    }
    // 清空输入框
    question.value = ""

    const res = await axios.post('http://121.43.122.26:3001/tongyi/video', msg)

    finish.value = true

    conversation.value.push({
        role: "assistant",
        content: res.data.content
    })
}



</script>

<style lang="css" scoped>
.bd {
  height: calc(100vh - 160px);
  background: #f5f7fa;
  overflow-y: auto;
  padding: 1rem 1rem 5rem;
  box-sizing: border-box;
}

.chat {
  margin: 1.5rem 0;
}

.bot.answer {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  gap: 0.8rem;
}
.bot.answer span {
  background: white;
  color: #333;
  padding: 1rem;
  border-radius: 0 1rem 1rem 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  max-width: 85%;
  line-height: 1.5;
  position: relative;
}

/* 视频容器 */
.bot.answer video {
  width: 100%;
  max-width: 600px;
  height: auto;
  border-radius: 0.5rem;
  display: block;
  margin-top: 0.5rem;
  aspect-ratio: 16/9;
  background: #000;
}

.user.question {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 0.8rem;
}
.user.question span {
  background: #2563EB;
  color: white;
  padding: 1rem;
  border-radius: 1rem 0 1rem 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  max-width: 85%;
  line-height: 1.5;
}

/* 角色标签 */
.bot.answer::before,
.user.question::before {
  content: attr(data-role);
  font-size: 0.8rem;
  color: #999;
  position: absolute;
  top: -1.2rem;
}

/* 加载动画 */
.van-loading {
  padding: 2rem;
  text-align: center;
  color: #666;
}
.van-icon-star-o {
  color: #2563EB;
  animation: spin 1.5s linear infinite;
}

@keyframes spin {
  100% { transform: rotate(360deg); }
}

.ft {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  padding: 1rem;
  border-top: 1px solid #eee;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
}

.input {
  display: flex;
  gap: 0.5rem;
  max-width: 800px;
  margin: 0 auto;
}

.input input {
  flex: 1;
  padding: 0.8rem 1.2rem;
  border: 1px solid #ddd;
  border-radius: 2rem;
  font-size: 1rem;
  background: #f8f9fa;
  transition: all 0.2s;
}

.input input:focus {
  outline: none;
  border-color: #2563EB;
  background: white;
  box-shadow: 0 0 0 3px rgba(142, 111, 247, 0.1);
}

.input button {
  padding: 0.8rem 1.5rem;
  background: #2563EB;
  color: white;
  border: none;
  border-radius: 2rem;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s;
}

.input button:active {
  opacity: 0.8;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .bd {
    padding: 0.5rem 0.5rem 4rem;
    height: calc(100vh - 115px);
  }
  
  .bot.answer span,
  .user.question span {
    max-width: 90%;
    font-size: 0.9rem;
    padding: 0.8rem;
  }
  
  .ft {
    padding: 0.8rem;
  }
  
  .input input {
    padding: 0.7rem 1rem;
    font-size: 0.9rem;
  }
  
  .input button {
    padding: 0.7rem 1.2rem;
  }

  .bot.answer video {
    max-width: 100%;
    max-height: 50vh;
  }
}
</style>